﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for message recipents data manipulation.
    /// </summary>
    public class MessagesRecipentsRepository : BaseRepository<MessageRecipent>, IMessagesRecipentsRepository
    {
        #region Fields

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="MessagesRecipentsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public MessagesRecipentsRepository(IDataServiceContext<MessageRecipent> dataServiceContext)
            :base(dataServiceContext)
        {

        }

        /// <summary>
        /// Deletes all message recipents from a message.
        /// </summary>
        /// <param name="messageId">The message id.</param>
        public void DeleteAllMessageRecipentsFromMessage(string messageId)
        {
            var messageRecipents = GetMessageRecipentsByMessage(messageId);

            messageRecipents.ForEach(mr => Delete(mr));
        }

        /// <summary>
        /// Gets the message recipents by message.
        /// </summary>
        /// <param name="messageId">The message id.</param>
        /// <returns>Colelction of message recipent of the given message id.</returns>
        public List<MessageRecipent> GetMessageRecipentsByMessage(string messageId)
        {
            return Entities.Where(mr => mr.Message == messageId).ToList();
        }

        /// <summary>
        /// Deletes all user message recipents.
        /// </summary>
        /// <param name="receiverID">The receiver ID.</param>
        public void DeleteAllReceiverMessageRecipents(string receiverID)
        {
            var messageRecipents = GetMessageRecipentsByReceiver(receiverID);

            messageRecipents.ForEach(mr => Delete(mr));
        }

        /// <summary>
        /// Gets the messages that the user is recipent of.
        /// </summary>
        /// <param name="receiverID">The receiver ID.</param>
        /// <returns>The messages that the user is recipent of.</returns>
        public List<MessageRecipent> GetMessageRecipentsByReceiver(string receiverID)
        {
            var messageRecipents = (from mr in Entities
                                    where mr.Recipent == receiverID
                                    select mr).ToList();

            return messageRecipents;
        }


    }
}
